package com.ruoyi.eam.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.eam.constants.EamConstants;
import com.ruoyi.eam.doman.*;
import com.ruoyi.eam.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eam.service.IEamLoanbackHService;

/**
 * 借用归还Service业务层处理
 *
 * @author xl
 * @date 2024-07-08
 */
@Service
public class EamLoanbackHServiceImpl implements IEamLoanbackHService
{
    @Autowired
    private EamLoanbackHMapper eamLoanbackHMapper;
    @Autowired
    private EamLoanbackDMapper eamLoanbackDMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EamAssetMapper assetMapper;

    /**
     * 查询借用归还
     *
     * @param loanBackId 借用归还主键
     * @return 借用归还
     */
    @Override
    public EamLoanbackH selectEamLoanbackHByLoanBackId(Long loanBackId)
    {
        EamLoanbackH eamLoanbackH = eamLoanbackHMapper.selectEamLoanbackHByLoanBackId(loanBackId);
        EamLoanbackD loanbackD = new EamLoanbackD();
        loanbackD.setLoanBackId(eamLoanbackH.getLoanBackId());
        List<EamLoanbackD> loanbackDS = eamLoanbackDMapper.selectEamLoanbackDList(loanbackD);
        List<EamAsset> assets = new ArrayList<>();
        for (EamLoanbackD chgD : loanbackDS) {
            EamAsset asset = assetMapper.selectEamAssetByAssetId(chgD.getAssetId());
            assets.add(asset);
        }
        eamLoanbackH.setLoanbackDS(assets);
        return eamLoanbackH;
    }

    /**
     * 查询借用归还列表
     *
     * @param eamLoanbackH 借用归还
     * @return 借用归还
     */
    @Override
    public List<EamLoanbackH> selectEamLoanbackHList(EamLoanbackH eamLoanbackH)
    {
        List<EamLoanbackH> list = eamLoanbackHMapper.selectEamLoanbackHList(eamLoanbackH);
        if(list.size() > 0){
            for (EamLoanbackH loanbackH : list) {
                loanbackH.setCreatorName(userMapper.selectUserByUserId(loanbackH.getCreator()).getNickName());
                loanbackH.setLoanCrewName(userMapper.selectUserByUserId(loanbackH.getLoanCrew()).getNickName());
                loanbackH.setLoanDeptName(deptMapper.selectDeptByDeptId(loanbackH.getLoanDept()).getDeptName());
                if(loanbackH.getBackAuditPerson() != null){
                    loanbackH.setBackAuditPersonName(userMapper.selectUserByUserId(loanbackH.getBackAuditPerson()).getNickName());
                }
                if(loanbackH.getAuditPerson() != null){
                    loanbackH.setAuditName(userMapper.selectUserByUserId(loanbackH.getAuditPerson()).getNickName());
                }
                if(loanbackH.getBackCreator() != null){
                    loanbackH.setBackCreatorName(userMapper.selectUserByUserId(loanbackH.getBackCreator()).getNickName());
                }
                EamLoanbackD loanbackD = new EamLoanbackD();
                loanbackD.setLoanBackId(loanbackH.getLoanBackId());
                List<EamLoanbackD> loanbackDS = eamLoanbackDMapper.selectEamLoanbackDList(loanbackD);
                List<EamAsset> assets = new ArrayList<>();
                for (EamLoanbackD chgD : loanbackDS) {
                    EamAsset asset = assetMapper.selectEamAssetByAssetId(chgD.getAssetId());
                    assets.add(asset);
                }
                loanbackH.setLoanbackDS(assets);
            }
        }
        return list;
    }

    /**
     * 新增借用归还
     *
     * @param eamLoanbackH 借用归还
     * @return 结果
     */
    @Override
    public int insertEamLoanbackH(EamLoanbackH eamLoanbackH)
    {
        //业务处理
        eamLoanbackH.setCreateTime(DateUtils.getNowDate());
        //借用编码规则
        //获取当前时间
        String nowDate = DateUtils.getDateYYYYMMDD();
        //获取最后一条借用编码
        String chgCodeLast = eamLoanbackHMapper.queryEamLoanbackHList().get(eamLoanbackHMapper.queryEamLoanbackHList().size() - 1).getLoanCode();
        //截取流水线
        String serial =  chgCodeLast.substring(11,17);
        if(serial.equals("999999")){
            serial = "000001";
        }else{
            int num = Integer.parseInt(serial) + 1;
            serial = String.format("%06d",num);
        }
        //最终编码
        String chgCode = EamConstants.JYD_CODE_PR + nowDate + serial;
        eamLoanbackH.setLoanCode(chgCode);
        eamLoanbackH.setIsdel(1L);
        eamLoanbackH.setReceiptsStatus(3L);
        eamLoanbackH.setIsLoanBack(1L);
        return eamLoanbackHMapper.insertEamLoanbackH(eamLoanbackH);
    }

    /**
     * 修改借用归还
     *
     * @param eamLoanbackH 借用归还
     * @return 结果
     */
    @Override
    public int updateEamLoanbackH(EamLoanbackH eamLoanbackH)
    {
        if(eamLoanbackH.getLoanCode().contains("GHD")){
            eamLoanbackH.setLoanCode(eamLoanbackH.getLoanCode().replace("GHD","JYD"));
        }
        return eamLoanbackHMapper.updateEamLoanbackH(eamLoanbackH);
    }

    /**
     * 批量删除借用归还
     *
     * @param loanBackIds 需要删除的借用归还主键
     * @return 结果
     */
    @Override
    public int deleteEamLoanbackHByLoanBackIds(Long[] loanBackIds)
    {
        return eamLoanbackHMapper.deleteEamLoanbackHByLoanBackIds(loanBackIds);
    }

    /**
     * 删除借用归还信息
     *
     * @param loanBackId 借用归还主键
     * @return 结果
     */
    @Override
    public int deleteEamLoanbackHByLoanBackId(EamLoanbackH eamLoanbackH)
    {
        return eamLoanbackHMapper.deleteEamLoanbackHByLoanBackId(eamLoanbackH);
    }
}
